package cn.senjoeson.triper.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.List;

import butterknife.BindView;
import cn.senjoeson.mylibrary.utilschanged.GlideUtils;
import cn.senjoeson.mylibrary.utilschanged.MyToast;
import cn.senjoeson.triper.R;
import cn.senjoeson.triper.base.BaseActivity;
import cn.senjoeson.triper.base.GreenDaoManager;
import cn.senjoeson.triper.bean.PlaceDetailBean;
import cn.senjoeson.triper.bean.PlaceDetailsBean;
import cn.senjoeson.triper.dao.PlaceDetailBeanDao;
import cn.senjoeson.triper.utils.ReadAssetFileUtils;

public class DestinationActivity extends BaseActivity {


    @BindView(R.id.left_arrow)
    ImageView mLeftArrow;
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.toolbarlayout)
    LinearLayout mToolbarlayout;
    @BindView(R.id.foreImage)
    ImageView mForeImage;
    @BindView(R.id.moreImage)
    TextView mMoreImage;
    @BindView(R.id.love)
    ImageView mLove;
    @BindView(R.id.add)
    ImageView mAdd;
    @BindView(R.id.linearLayout)
    LinearLayout mLinearLayout;
    public PlaceDetailBean mPlaceDetailBean;
    @BindView(R.id.address)
    TextView mAddress;
    @BindView(R.id.opentime)
    TextView mOpentime;
    @BindView(R.id.tel)
    TextView mTel;
    @BindView(R.id.avgstaytime)
    TextView mAvgstaytime;
    @BindView(R.id.website)
    TextView mWebSite;
    @BindView(R.id.introduce)
    TextView mIntroduce;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_destination;
    }

    @Override
    protected void initView() {

        List<PlaceDetailBean> placeDetailBeans = getPlaceDetailBeans();
        if (placeDetailBeans.size() > 0) {
            mPlaceDetailBean = placeDetailBeans.get(0);
            String imageUrl = mPlaceDetailBean.getImageUrl();
            if (imageUrl.contains(",")) {
                String[] imageUrls = imageUrl.split(",");
                GlideUtils.loadImage(this, imageUrls[0], mForeImage);
            } else {
                GlideUtils.loadImage(this, imageUrl, mForeImage);
            }
            mTitle.setText(mPlaceDetailBean.getPlaceName());
            mAddress.setText(mPlaceDetailBean.getAddress());
            mAvgstaytime.setText(mPlaceDetailBean.getAvgStayTime());
            mOpentime.setText(mPlaceDetailBean.getOpenTime());
            mTel.setText(mPlaceDetailBean.getTel());
            mWebSite.setText(mPlaceDetailBean.getWebsite());
            mIntroduce.setText(mPlaceDetailBean.getIntroduction());
        }


    }

    private List<PlaceDetailBean> getPlaceDetailBeans() {
        String fileContent = ReadAssetFileUtils.readFile(this, "placedetails.json");
        PlaceDetailsBean placeDetailsBean = new Gson().fromJson(fileContent, PlaceDetailsBean.class);
        long placeId = getIntent().getLongExtra("placeId", -1);
        PlaceDetailBeanDao placeDetailBeanDao = GreenDaoManager.getInstance().getNewSession().getPlaceDetailBeanDao();
        List<PlaceDetailBean> placedetails = placeDetailsBean.getPlacedetails();
        for (int i = 0; i < placedetails.size(); i++) {
            if (placeDetailBeanDao.load(placedetails.get(i).getDetailId()) == null) {
                placeDetailBeanDao.insert(placedetails.get(i));
            }
        }
        return placeDetailBeanDao.queryBuilder().where(PlaceDetailBeanDao.Properties.PlaceId.eq(placeId)).build().list();
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        mLeftArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mLove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击时 判断是否在Favorite已经存在,如果有,就给用户一个提示,如果没有,就添加,并提示用户已经添加
                addToFavorite();
            }
        });
        mAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addtoTripContent();
            }
        });
        mMoreImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DestinationActivity.this, PhotoViewActivity.class);
                intent.putExtra("imageUrls",mPlaceDetailBean.getImageUrl());
                startActivity(intent);
            }
        });
    }

    /**
     * 添加至内容页
     */
    private void addtoTripContent() {
        //todo 需要连接数据库插入到placebean 等待客户确定需求
        MyToast.showToast("添加到内容地点的列表中,但未指定某个时间,故暂不实现");
    }

    private void addToFavorite() {
        List<PlaceDetailBean> placeDetailBeans = getPlaceDetailBeans();
        if (placeDetailBeans != null) {
            MyToast.showToast("Favorite列表已经存在了哦!~");
            return;
        } else {
            //todo 添加到Favorite中
            MyToast.showToast("添加到收藏页,但数据都是假的,暂不实现");
        }
    }


}
